﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EventDefine
{
    public const int OnRoomListUpdate = 8001; //房间信息更新
    public const int OnOtherJoinRoom = 8002;//其他人加入房间
    public const int OnOtherLeaveRoom = 8003;//其他人离开房间
}
public class EventObject
{
    public object msgBody;

    public Dictionary<string, object> msg_dic;

    public EventObject()
    {

    }
    public EventObject(object body)
    {
        msgBody = body;
    }
    public void AddObject(string key,object obj)
    {
        if(!msg_dic.ContainsKey(key))
        {
            msg_dic.Add(key, obj);
        }
        else
        {
            msg_dic[key] = obj;
        }
    }
}
public class EventCentre : SingletonMono<EventCentre>
{
    Dictionary<int, List<EventFun>> eventContainer = new Dictionary<int, List<EventFun>>();
    public delegate void EventFun(EventObject msg);
    public void AddEventListener(int eventId, EventFun fun)
    {
        if (!eventContainer.ContainsKey(eventId))
        {
            var list_event = new List<EventFun>();
            list_event.Add(fun);
            eventContainer.Add(eventId, list_event);
        }
        else
        {
            var list_event = eventContainer[eventId];
            list_event.Add(fun);
        }
    }

    public void RemoveEvent(int eventId, EventFun fun)
    {
        if (!eventContainer.ContainsKey(eventId))
            return;

        var list_event = eventContainer[eventId];
        var list_index = list_event.FindIndex(x => x == fun);
        if (list_index >= 0)
        {
            list_event.RemoveAt(list_index);
        }
    }

    public void NotifyEvent(int eventId, EventObject msgObj)
    {
        if (!eventContainer.ContainsKey(eventId))
            return;

        var list_event = eventContainer[eventId];
        foreach (var evn in list_event)
        {
            if (evn != null)
                evn(msgObj);
        }
    }
}


